package com.newyear.concertManagementSystem.servlet;

import entity.Concert;
import service.ConcertService;
import service.impl.ConcertServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        ConcertService concertService = new ConcertServiceImpl();

        Concert concert = new Concert();
        concert.setConcertName(request.getParameter("concertName"));
        concert.setCity(request.getParameter("city"));
        concert.setPrice(Float.parseFloat(request.getParameter("price")));
        String recommend = request.getParameter("recommend");
        concert.setRecommend(Integer.parseInt(recommend));
        concert.setStartTime(request.getParameter("startTime"));
        concert.setAddress(request.getParameter("address"));

        int add = concertService.add(concert);

        PrintWriter out = response.getWriter();

        if (add > 0){
            out.println("<script>alert('添加成功！');location.href = 'page?currentPage=1&pageSize=3'</script>");
        }else {
            out.println("<script>alert('添加失败！');history.back()</script>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
